package cn.libinal.scloud.base.oauth.security;

import cn.libinal.scloud.base.common.dto.ClientDto;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.provider.ClientDetails;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Description: 自定义客户端信息
 *
 * @author libinal
 * Created on 2019/3/15 10:44
 **/
@Data
public class CustomClientDetails implements ClientDetails {

    private ClientDto client;
    private Set<String> scope;

    /**
     * 构造方法
     * @param client 客户端信息
     */
    CustomClientDetails(ClientDto client) {
        this.client = client;
    }

    @Override
    public String getClientId() {
        return client.getClientId();
    }

    @Override
    public Set<String> getResourceIds() {
        if (StringUtils.isNotBlank(client.getResourceIds())) {
            return Stream.of(StringUtils.split(client.getResourceIds(), ","))
                    .collect(Collectors.toSet());
        }
        return null;
    }

    @Override
    public boolean isSecretRequired() {
        return client.getIsSecretRequired();
    }

    @Override
    public String getClientSecret() {
        return client.getClientSecret();
    }

    @Override
    public boolean isScoped() {
        return client.getIsScoped();
    }

    @Override
    public Set<String> getScope() {
        if (StringUtils.isNotBlank(client.getScope())) {
            return Stream.of(StringUtils.split(client.getScope(), ","))
                    .collect(Collectors.toSet());
        }
        return null;
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        if (StringUtils.isNotBlank(client.getAuthorizedGrantTypes())) {
            return Stream.of(StringUtils.split(client.getAuthorizedGrantTypes(), ","))
                    .collect(Collectors.toSet());
        }
        return null;
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        if (StringUtils.isNotBlank(client.getRegisteredRedirectUri())) {
            return Stream.of(StringUtils.split(client.getRegisteredRedirectUri(), ","))
                    .collect(Collectors.toSet());
        }
        return null;
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        if (StringUtils.isNotBlank(client.getAuthorities())
                && StringUtils.length(client.getAuthorities()) > 0) {
            return AuthorityUtils.commaSeparatedStringToAuthorityList(client.getAuthorities());
        }
        return null;
    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return client.getAccessTokenValidity();
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return client.getRefreshTokenValidity();
    }

    @Override
    public boolean isAutoApprove(String scope) {
        return client.getIsAutoApprove();
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        return null;
    }
}
